package controllers

import (
	"encoding/json"
	// "errors"
	// "strconv"
	// "strings"
	admin "gitee.com/lzqysoft/goadmin/controllers"
	// "goadmin/libs/encrypt"
	"yjk/models"

	"github.com/astaxie/beego"
)

// PresalesController operations for Presales
type PresalesController struct {
	admin.CommonController
}

// URLMapping ...
func (c *PresalesController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Post
// @Description create Presales
// @Param	body		body 	models.Presales	true		"body for Presales content"
// @Success 201 {int} models.Presales
// @Failure 403 body is empty
// @router / [post]
func (c *PresalesController) Post() {
	var v models.Presales
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if _, err := models.AddPresales(&v); err == nil {
			c.Ctx.Output.SetStatus(201)
			c.Data["json"] = v
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// GetOne ...
// @Title Get One
// @Description get Presales by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Presales
// @Failure 403 :id is empty
// @router /:id [get]
func (c *PresalesController) GetOne() {
	id := c.Ctx.Input.Param(":id")
	v, err := models.GetPresalesByPreorder(id)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = v
	}
	c.ServeJSON()
}

// GetAll ...
// @Title Get All by preorder
// @Description get Presales
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Presales
// @Failure 403
// @router /GetAll/:id [get]
func (c *PresalesController) GetAll() {
	id := c.Ctx.Input.Param(":id")
	beego.Debug(id)
	v, err := models.GetPresalesByPreorder(id)
	if err != nil {
		beego.Error(err)
		c.RetError(admin.ErrNoData)
	}
	c.RetSuccess(v)
}

// Put ...
// @Title Put
// @Description update the Presales
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Presales	true		"body for Presales content"
// @Success 200 {object} models.Presales
// @Failure 403 :id is not int
// @router /:id [put]
func (c *PresalesController) Put() {
	id := c.Ctx.Input.Param(":id")
	v := models.Presales{Id: id}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if err := models.UpdatePresalesById(&v); err == nil {
			c.Data["json"] = "OK"
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the Presales
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *PresalesController) Delete() {
	id := c.Ctx.Input.Param(":id")

	if err := models.DeletePresales(id); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}
